//
//  BJTableViewCell.m
//  WildDonkeySectionOne
//
//  Created by 黎传炎 on 15/3/28.
//  Copyright (c) 2015年 lanou. All rights reserved.
//

#import "BJTableViewCell.h"
#import "BJData.h"
#import "UIImageView+WebCache.h"

@implementation BJTableViewCell
- (void)dealloc
{
    [_photocdn_img release];
    [_time_at release];
    [_user release];
    [_user_image release];
    [_imgtext release];
    [_view release];
    [_bjdata release];
    [super dealloc];
}
- (void)setBjdata:(BJData *)bjdata
{
    if (_bjdata != bjdata) {
        [_bjdata release];
        _bjdata = [bjdata retain];
        
        NSString *str = [bjdata.created_at substringWithRange:NSMakeRange(0, 16)];
        [_user_image setImageWithURL:[NSURL URLWithString:bjdata.profile_image]];
        
        //_height = bjdata.height;
        _width = bjdata.width;
        _height = (self.contentView.bounds.size.width - 10) * (bjdata.height) / _width;
            //self.contentView.bounds.size.width
            if (bjdata.width >= [UIScreen mainScreen].bounds.size.width) {
                //NSLog(@"屏幕宽度是%f",[UIScreen mainScreen].bounds.size.width);
                _photocdn_img.frame = CGRectMake(5, 60 +[BJTableViewCell heightForString:bjdata.text],[UIScreen mainScreen].bounds.size.width - 10,_height);
            }else{
                _photocdn_img.frame = CGRectMake(([UIScreen mainScreen].bounds.size.width - _width - 10)/2 + 10, 60 +[BJTableViewCell heightForString:bjdata.text], _width,bjdata.height);
            }
        
        _user.text = bjdata.name;
        _user.textColor = [UIColor orangeColor];
        _imgtext.text  = bjdata.text;
        _imgtext.frame = CGRectMake(5, 50, self.contentView.bounds.size.width, [BJTableViewCell heightForString:bjdata.text]);
        [_photocdn_img setImageWithURL:[NSURL URLWithString:bjdata.cdn_img]];
        
        
        
        _time_at.text =[NSString stringWithFormat:@"%@",str];
        
        
    }
    
}
//- (CGSize)boundingRectWithSize:(CGSize)size{
//    NSDictionary *attribute = @{NSFontAttributeName:self.font};
//    CGSize retSize = [self.text boundingRectWithSize:size options:NSStringDrawingTruncatesLastVisibleLine |
//                      NSStringDrawingUsesLineFragmentOrigin |
//                      NSStringDrawingUsesFontLeading  attributes:attribute context:nil].size;
//    return retSize;
//}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(5, 5, 40, 40)];
        imageView.layer.cornerRadius = 20;
        imageView.layer.masksToBounds =YES;
        self.user_image = imageView;
        [self.contentView addSubview:_user_image];
        [imageView release];
        
        _user = [[UILabel alloc]initWithFrame:CGRectMake(50, 5, 80, 30)];
        _user.font = [UIFont systemFontOfSize:15];
        [self.contentView addSubview:_user];
        
        _time_at = [[UILabel alloc]initWithFrame:CGRectMake(50, 30, 150, 20)];
        _time_at.font = [UIFont systemFontOfSize:10];
        [self.contentView addSubview:_time_at];
        
        _imgtext = [[UILabel alloc]initWithFrame:CGRectMake(5, 40, 300, 80)];
        _imgtext.numberOfLines = 0;
        _imgtext.font = [UIFont systemFontOfSize:15];
        _imgtext.textAlignment = NSTextAlignmentNatural;
        [self.contentView addSubview:_imgtext];
        
        UIImageView *image1 = [[UIImageView alloc]initWithFrame:(CGRectMake(5, 130, 0, 0))];
        self.photocdn_img = image1;
        [self.contentView addSubview:_photocdn_img];
        [image1 release];
        
    }
    return self;
}

+ (CGFloat)heightForString:(NSString *)string
{
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:15],NSFontAttributeName, nil];
    CGRect bounds = [string boundingRectWithSize:CGSizeMake([UIScreen mainScreen].bounds.size.width, 1000) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    NSLog(@"字体高度：%g",bounds.size.height);
    return bounds.size.height;
}





- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
